<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Company extends CI_Controller {

    public function index()
    {
        $this->load->model('Company_model');
        $ruc = null;
        $name = null;
        $address = null;
        $telephone = null;
        $email = null;
        $datei=null;
       
       
        
        
      
        if($this->input->post('ruc')){
            $ruc =$this->input->post('ruc');
        }
        if($this->input->post('name')){
            $name = $this->input->post('name');
        }
        if($this->input->post('address')){
            $address = $this->input->post('address');
        }
        if($this->input->post('telephone')){
            $telephone = $this->input->post('telephone');
        }
         if($this->input->post('telephone')){
            $email= $this->input->post('telephone');
        }
        if($this->input->post('create')){
            $datei =$this->input->post('create');
        }
                
        $company = $this->Company_model->getAll();
        if ($ruc != null || $name != null || $address != null || $telephone != null || $email != null || $datei != null) {
	        $company = $this->Company_model->search($ruc, $name, $address, $telephone, $email,$datei);
	    }
      
      


     
        $vars['ruc'] = $ruc;
		$vars['name'] = $name;
		$vars['address'] = $address;
        $vars['telephone'] = $telephone;
        $vars['email'] = $email;
       $data['create'] = $datei;
        
        
        $vars['content_view']='company_list';
        $vars['company'] = $company;
        $vars['menu']='Company';
        $this->load->view('template', $vars);
         
    }
    
	
    public function add($success_message = null)
    {
       // ***** /
                
	   if (isset($success_message)){
		$vars['success_message'] = true;
	   }
                                
       $vars['content_view'] = 'company_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'Company'; //
	   $this->load->view('template', $vars);
            
       //*********/
    }
    
   
     public function edit($code)
     {
        // ***** /
        $this->load->model('Company_model');
           
       $vars['company'] = $this->Company_model->find($code);
           
       $vars['content_view'] = 'company_form'; // varibale cque contiene el nombre del formulario  llamar
	   $vars['menu'] = 'Company'; //
	   $this->load->view('template', $vars);
       
            
       /*********/
    }
    
    
    public function submit()
    {
         if( $this->input->post('id_company') != ""){
             
             $this->load->model('Company_model');
              
             $id = $this->input->post('id_company');
            
			 $vars['name'] = $this->input->post('name');
			 $vars['ruc'] = $this->input->post('ruc');
			 $vars['address'] = $this->input->post('address');
			   
			 $vars['telephone'] = $this->input->post('telephone');
			 $vars['email'] = $this->input->post('email');
             $vars['create'] = $this->input->post('datei');
             
             $vars['edit'] = $this->Company_model->update($id, $vars);
             
             $vars['company'] = $this->Company_model->find($id);
             $vars['content_view'] = 'company_form'; // varibale cque contiene el nombre del formulario  llamar
             $vars['menu'] = 'Company'; //
             $this->load->view('template', $vars);
            
             
            }else{
                
                
             $this->load->model('Company_model');
                
             $data= array();
               
	        $data['name'] = $this->input->post('name');
	        $data['ruc'] = $this->input->post('ruc');
	        $data['address'] = $this->input->post('address');
	       
	        $data['telephone'] = $this->input->post('telephone');
	        $data['email'] = $this->input->post('email');
             $data['create'] = $this->input->post('datei');
	        $data['status'] = 1;
	        
                
                
                
                
                $vars['success'] = $this->Company_model->add($data); //despues de agregar
                
         
                $vars['content_view'] = 'company_form'; // varibale cque contiene el nombre del formulario  llamar
                $vars['menu'] = 'Company'; //
                $this->load->view('template', $vars);
//                $this->index();
                 
            }
         
     
    }

    public function delete($id)
    {
        $this->load->model('Company_model');
        $this->Company_model->delete($id);
        $this->index();
       
      
       
    }
    
}